package com.bazaaroid.server.gae.core.dao.partner.impl;

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Date;

import javax.jdo.PersistenceManager;
import javax.jdo.Query;

import au.com.bytecode.opencsv.CSVReader;

import com.bazaaroid.server.gae.core.dao.impl.JDOGenericDAOImpl;
import com.bazaaroid.server.gae.core.dao.partner.PartnerDAO;
import com.bazaaroid.server.gae.core.enums.Languages;
import com.bazaaroid.server.gae.core.persistence.model.partner.Contact;
import com.bazaaroid.server.gae.core.persistence.model.partner.Partner;
import com.bazaaroid.server.gae.core.persistence.model.partner.Product;
import com.google.appengine.api.datastore.Email;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.Link;
import com.google.appengine.api.datastore.PhoneNumber;

public class JDOPartnerDAOImpl extends JDOGenericDAOImpl<Partner, Key>
		implements PartnerDAO {

	public Partner getPartnerByName(String name) {
		PersistenceManager persistenceManager = getPersistenceManager();
		persistenceManager.setDetachAllOnCommit(true);
		Partner partner = null;

		Query query = persistenceManager.newQuery(getEntityClass());
		query.setFilter("name == nameParam");
		query.declareParameters("String nameParam");
		query.setUnique(true);

		try {
			partner = (Partner) query.execute(name);
		}
		finally {
			query.closeAll();
			persistenceManager.close();
		}

		return partner;
	}

	public Partner getPartnerByProduct(Product product) {
		PersistenceManager persistenceManager = getPersistenceManager();
		persistenceManager.setDetachAllOnCommit(true);
		Partner partner = null;

		Query query = persistenceManager.newQuery(Product.class);
		query.setResult("partner");
		query.declareImports("import com.google.appengine.api.datastore.Key;");
		query.setFilter("key == keyParam");
		query.declareParameters("Key keyParam");
		query.setUnique(true);

		try {
			partner = (Partner) query.execute(product.getKey());
			partner.getContact();
		}
		finally {
			query.closeAll();
			persistenceManager.close();
		}

		return partner;
	}

	@Override
	public void importPartnersFromFile(String filePath) {
		CSVReader csvReader = null;

		try {
			InputStreamReader inputStreamReader = new InputStreamReader(
					new BufferedInputStream(new FileInputStream(filePath)), "UTF-8");

			csvReader = new CSVReader(inputStreamReader, ';', '\"', 2);

			Date today = new Date();
			String [] nextLine;

			while ((nextLine = csvReader.readNext()) != null) {
				int index = 0;

				String name = nextLine[index++].trim();
				String description = nextLine[index++].trim();
				String telephone = nextLine[index++].trim();
				String mobile = nextLine[index++].trim();
				String fax = nextLine[index++].trim();
				String email = nextLine[index++].trim();
				String site = nextLine[index++].trim();
				String language = nextLine[index++].trim();

				Partner partner = getPartnerByName(name);

				if(partner == null) {
					partner = new Partner();
					partner.setName(name);
					partner.setCreated(today);
			   	}

				if(Languages.isSupportedLanguage(language))
					partner.setLanguage(language);
				else
					partner.setLanguage(null);

				partner.setModified(today);
				partner.setDescription(description);

				Contact contact = new Contact();
				contact.setTelephone(new PhoneNumber(telephone));
				contact.setMobile(new PhoneNumber(mobile));
				contact.setFax(fax);
				contact.setEmail(new Email(email));
				contact.setSite(new Link(site));
				partner.setContact(contact);
				makePersistent(partner);
			}
			csvReader.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		
	}
}
